package com.oyjd.ybx.business.talk.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.oyjd.ybx.bean.po.Talk;
import com.oyjd.ybx.business.talk.vo.CustVO;
import com.oyjd.ybx.business.talk.vo.TalkExVO;
import com.oyjd.ybx.fw.dao.BaseDao;

/**
 * 
 * 在线交流数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class TalkDao extends BaseDao {
	/**
	 * 获取在线交流列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TalkExVO> getList(String talkAccount, String getAccount) {
		String hqlResult = "select new " + TalkExVO.class.getName() + TalkExVO.STRUCT;
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Talk.class.getName()).append(" talk");
		sb.append(" where 1=1");
		sb.append(" and  ((talk.talkAccount=? and talk.getAccount=?) or (talk.talkAccount=? and talk.getAccount=?) )");
		sb.append(" order by talk.createTime desc");
		List<Object> ps = new ArrayList<Object>();
		ps.add(talkAccount);
		ps.add(getAccount);
		ps.add(getAccount);
		ps.add(talkAccount);

		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(0);
		q.setMaxResults(10);
		return q.list();
	}

	/**
	 * 获取与专家对话的人物列表
	 * 
	 * @param account
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<CustVO> getTalkerForPro(String account) {
		String hql = "select new " + CustVO.class.getName() + "(t.talkAccount,count(t.id)) from " + Talk.class.getName() + " t where t.getAccount=? group by t.talkAccount";
		Query q = getSession().createQuery(hql);
		q.setParameter(0, account);
		return q.list();
	}

}
